var express = require('express');
var router = express.Router();
var multiparty = require('multiparty')
var jwt = require('jsonwebtoken')
var {userModel,companyModule,careerModule} = require('../model/model')
<<<<<<< HEAD
=======

>>>>>>> 6e5d5ac345ccf466ba978593f7596ade10a97006
router.get('/', function(req, res, next) {
  res.send('respond with a resource');
});

router.get('/mebasic',async(req,res)=>{
  let {id} = req.query
  if(!id){
    res.send({
      code:1,
      msg:'参数错误'
    })
    return
  }
  userModel.findOne({_id:id}).then(data=>{
    res.send({
      code:0,
      data
    })
  })
})

router.post('/login',async(req,res)=>{
  let {username,password} = req.body
  let result = await userModel.findOne({username,password})
  if(result){
    let token = jwt.sign({username},'123456',{expiresIn:60*60*24})
    res.send({
      code:0,
      data:result,
      token
    })
  }else{
    res.send({
      code:1,
      msg:'用户名或密码错误'
    })
  }
})

router.post('/upload', function (req, res) {
  let form = new multiparty.Form();
  form.uploadDir = 'upload';
  form.parse(req, (error, fields, file) => {
      if (error) {
          // 如果解析出现错误，返回错误状态码和信息给前端
          return res.status(500).send({
              code: 500,
              message: '文件上传解析出错：' + error.message
          });
      }
      res.send({
          code: 200,
          path: 'http://127.0.0.1:3000/' + file.file[0].path
      });
  });
});
// 公司
router.get('/company',async function(req, res, next) {

  let list=await companyModule.find()
  res.send({
    code:200,
    data:list
  })
});
router.post('/audio', async (req, res) => {
   let audio = req.body.audio
   userModel.updateOne({_id:req.body._id},{audio}).then(data=>{
    res.send({
      code:200,
      data:req.data
    })
   })
   
});
// 职业
router.get('/career',async function(req, res, next) {

  let list=await careerModule.find().populate("typeId")
  res.send({
    code:200,
    data:list
  })
});
router.post('/update',async (req,res)=>{
  let {id,...data} = req.body
  console.log(data);
  
  await userModel.updateOne({_id:id},data).then(data=>{
    res.send({
      code:200,
      data:req.data
    })
  })
})
router.post('/updateobj',async (req,res)=>{
  let id = req.query.id
  let cate = req.query.cate
  await userModel.updateOne({_id:id},{work:req.body}).then(data=>{
    res.send({
      code:200,
      data:req.data
    })
  })
})
router.post('/updatestudy',async (req,res)=>{
  let id = req.query.id
  await userModel.updateOne({_id:id},{study:req.body}).then(data=>{
    res.send({
      code:200,
      data:req.data
    })
  })
})

<<<<<<< HEAD
=======
// 公司
router.get('/company',async function(req, res, next) {

  let list=await companyModule.find()
  res.send({
    code:200,
    data:list
  })
});



// 职业
router.get('/career',async function(req, res, next) {

  let list=await careerModule.find().populate("typeId")
  res.send({
    code:200,
    data:list
  })
});

>>>>>>> 6e5d5ac345ccf466ba978593f7596ade10a97006
module.exports = router;